<template>

  <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="80%"
      append-to-body
      v-loading.fullscreen.lock="loadTaskInfoLoading"
      :before-close="dialogClose">
    <avue-tabs :option="tableOptions" @change="optionChange"></avue-tabs>
    <avue-form v-if="!!activitiModelFormConfig" :option="activitiModelFormConfig" v-model="activitiModelFormData"></avue-form>
    <avue-form v-if="!!activitiModelFormConfig" :option="taskHandlerFormOption" v-model="taskHandlerFormData"></avue-form>
    <el-row style="text-align: center">
      <el-button @click="dialogVisible=false">取消</el-button>
      <el-button @click="taskHandlerReject" plain icon="el-icon-circle-close" type="danger">驳回</el-button>
      <el-button @click="taskHandlerAgree" icon="el-icon-check" type="success">同意</el-button>
    </el-row>
  </el-dialog>

</template>

<script>
import userTaskApi, {getHandlerTaskInfo, taskHandlerReject} from "@/api/start/activiti/userTaskApi";
import activitiFormUtils from "@/option/activitiFormUtils";
import {getTableOption} from "@/api/start/formEngine/formApi";
import {systemDictionary} from "@/const/systemConstant";
import {getUserInfo} from "@/api/sys/userApi";

export default {
  name: "userHandlerTask",
  computed: {
    dialogTitle() {
      return `【${this.activitiStartUserName}】的【${this.processInstanceName}】流程任务处理`;
    },
    tableOptions() {
      return {
        column: [
          {
            icon: 'el-icon-warning',
            label: '表单信息',
            prop: 'formData',
          }
        ]
      }
    },
    taskHandlerFormOption() {
      return {
        emptyBtn: false,
        submitBtn: false,
        column: [
          {
            row: true,
            span: 24,
            label: '处理意见',
            prop: 'handlerOpinion',
            type: "textarea",
          }
        ]
      }
    },
    activitiStartUserName() {
      let row = this.activitiStartUserInfo;
      if (!row) return;
      if (row.nickName) {
        return row.nickName;
      } else if (row.realName) {
        return row.realName
      }
      return row.userName;
      return
    },
    processInstanceName() {
      if (!this.processInstanceInfo) {
        return "";
      }
      return this.processInstanceInfo.processDefinitionName;
    }
  },
  data() {
    return {
      dialogVisible: false,
      currentOption: null,
      activitiModelFormConfig: null,
      activitiModelFormData: {},
      activitiStartUserInfo: {},
      processInstanceInfo: {},
      taskHandlerFormData: {handlerOpinion: ''},
      loadTaskInfoLoading: true,
      taskId: null,
    }
  },
  created() {
    this.loadTaskInfoLoading = false;
    this.currentOption = this.tableOptions.column[0];
  },
  methods: {
    optionChange(column) {
      this.currentOption = column;
    },
    dialogClose() {
      this.dialogVisible = false;
      this.loadTaskInfoLoading = false;
      this.taskId = null;
    },
    show(row) {
      this.dialogVisible = true;
      this.loadTaskInfoLoading = true;
      this.loadTaskInfo(row);
    },
    loadTaskInfo(row) {
      this.taskId = row.id;
      getHandlerTaskInfo(row.id).then(res => {
        let data = res.data.data;
        this.processInstanceInfo = data.processInstanceInfo;
        this.activitiModelFormData = data.activitiModelData;
        if (data.startUserId) {
          getUserInfo(data.startUserId).then(res => {
            this.activitiStartUserInfo = res.data.data;
          })
        }
        let activitiModelConfig = data.activitiModelConfig;
        if (activitiModelConfig && activitiModelConfig.formType === systemDictionary.activitiFormTypes.systemDynamicForm) {
          this.loadActivitiModelForm(activitiModelConfig.formValue);
        }

      }).catch(err => {
        this.$message.error('任务信息加载失败请联系管理员');
      }).finally(() => this.loadTaskInfoLoading = false)
    },
    loadActivitiModelForm(formId) {
      getTableOption(formId).then(res => {
        let data = res.data.data;
        data = JSON.parse(data)
        data = activitiFormUtils.activitiHandlerTaskMainForm(data)
        console.log("当前表单", data)
        this.activitiModelFormConfig = data;
      })
    },
    /**
     * 同意
     */
    taskHandlerAgree() {
      userTaskApi.taskHandlerAgree(this.taskId, this.taskHandlerFormData.handlerOpinion).then(res => {
        this.$message.success('任务处理成功');
        this.$emit("refresh")
        this.dialogClose()
      })
    },
    /**
     * 流程驳回
     */
    taskHandlerReject() {
      userTaskApi.taskHandlerReject(this.taskId, this.taskHandlerFormData.handlerOpinion).then(res => {
        this.$message.success('任务处理成功');
        this.$emit("refresh")
        this.dialogClose()
      })
    }
  }
}
</script>

<style scoped>

</style>
